package org.example.springboot.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.example.springboot.entity.SysConfig;
import org.example.springboot.mapper.SysConfigMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统配置服务类
 */
@Service
public class SysConfigService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SysConfigService.class);
    
    @Resource
    private SysConfigMapper sysConfigMapper;
    
    /**
     * 获取系统配置值
     * @param key 配置键
     * @param defaultValue 默认值
     * @return 配置值
     */
    public String getConfigValue(String key, String defaultValue) {
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysConfig::getConfigKey, key);
        
        SysConfig config = sysConfigMapper.selectOne(queryWrapper);
        return config != null ? config.getConfigValue() : defaultValue;
    }
    
    /**
     * 更新系统配置
     * @param key 配置键
     * @param value 配置值
     */
    public void updateConfigValue(String key, String value) {
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysConfig::getConfigKey, key);
        
        SysConfig config = sysConfigMapper.selectOne(queryWrapper);
        if (config != null) {
            config.setConfigValue(value);
            sysConfigMapper.updateById(config);
        } else {
            config = new SysConfig();
            config.setConfigKey(key);
            config.setConfigValue(value);
            config.setDescription(key);
            sysConfigMapper.insert(config);
        }
    }
    
    /**
     * 获取所有系统配置
     * @return 配置列表
     */
    public List<SysConfig> getAllConfigs() {
        LOGGER.info("查询所有系统配置");
        return sysConfigMapper.selectList(null);
    }
    
    /**
     * 根据KEY获取配置
     * @param key 配置键
     * @return 配置信息
     */
    public SysConfig getConfigByKey(String key) {
        LOGGER.info("根据KEY查询配置: {}", key);
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysConfig::getConfigKey, key);
        return sysConfigMapper.selectOne(queryWrapper);
    }
    
    /**
     * 更新配置
     * @param config 配置信息
     * @return 是否成功
     */
    public boolean updateConfig(SysConfig config) {
        LOGGER.info("更新配置: {}", config.getConfigKey());
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysConfig::getConfigKey, config.getConfigKey());
        
        SysConfig existConfig = sysConfigMapper.selectOne(queryWrapper);
        if (existConfig != null) {
            config.setId(existConfig.getId()); // 设置ID，确保更新正确的记录
            return sysConfigMapper.updateById(config) > 0;
        }
        return false;
    }
    
    /**
     * 添加配置
     * @param config 配置信息
     * @return 是否成功
     */
    public boolean addConfig(SysConfig config) {
        LOGGER.info("添加配置: {}", config.getConfigKey());
        return sysConfigMapper.insert(config) > 0;
    }
    
    /**
     * 删除配置
     * @param key 配置键
     * @return 是否成功
     */
    public boolean deleteConfig(String key) {
        LOGGER.info("删除配置: {}", key);
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysConfig::getConfigKey, key);
        return sysConfigMapper.delete(queryWrapper) > 0;
    }
} 